# -*- coding:utf-8 -*-
from django import forms
from aventon.traveling.models import Vehiculo, Viajero, Viaje
from django.contrib.auth.models import User
from django.forms.widgets import PasswordInput

class VehiculoForm(forms.ModelForm):
    class Meta:
        model = Vehiculo

class ViajeForm(forms.ModelForm):
    class Meta:
        model = Viaje
        exclude = ('viajero',)

class ViajeroForm(forms.ModelForm):
    class Meta:
        model = Viajero
        fields = ('documento','telefono',)


class UserForm(forms.ModelForm):
    first_name = forms.CharField(max_length=30, required=True, label=u'Nombre')
    last_name = forms.CharField(max_length=30, required=True, label=u'Appellido')
    email = forms.EmailField(required=True)
    email_check = forms.EmailField(required=True, label=u'Repetir Email')
    password = forms.CharField(max_length=30, required=True, label=u'Clave', widget=PasswordInput)
    password_check = forms.CharField(max_length=30, required=True, label=u'Repetir Clave', widget=PasswordInput)

    def clean_password_check(self):
        check = self.data['password_check']
        if check != self.data['password']:
            raise forms.ValidationError(u'Las claves no coinciden')
        else:
            return check

    def clean_email(self):
        email = self.data['email']
        if User.objects.filter(email=email).count() > 0 or User.objects.filter(username=email).count() > 0:
            raise forms.ValidationError(u'El e-mail ya está en uso')
        else:
            return email

    def clean_email_check(self):
        check = self.data['email_check']
        if check != self.data['email']:
            raise forms.ValidationError(u'Los e-mails no coinciden')
        else:
            return check

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'email_check', 'password', 'password_check')